package bj.gui

import scala.swing.event
import event.ButtonClicked
import scala.swing.Frame
import scala.swing.Label
import scala.swing.Swing
import scala.swing.Button
import scala.swing.FlowPanel
import scala.swing.GridPanel
import scala.swing.BorderPanel
import scala.swing.Component
import javax.swing.SpinnerNumberModel
import javax.swing.JSpinner
import scala.swing.TextField
import javax.swing.ImageIcon
import bj.actor.House
import bj.actor.Table
import bj.actor.AddTable
import bj.actor.Send

class CreateTable () extends Frame {
  
  javax.swing.UIManager.setLookAndFeel(Constants.LF)
  println("CreateTable window opened")
    title = "Blackjack - Create Table"
    minimumSize = Constants.EditTableSize

    val dealerField = new TextField {
      columns = 8
      text = "House Dealer"
    }
    val minSpinner = new JSpinner(new SpinnerNumberModel(20,1,5000,1))
    
    val maxSpinner = new JSpinner(new SpinnerNumberModel(50,1,5000,1))
    
    val okButton = new Button { text = "OK" }
    val cancelButton = new Button { text = "Cancel" }

    val labelPanel = new FlowPanel() {
      contents += new Label{ text = "Create Table" }
      background = Constants.greenBack
    }
    val minPanel = new FlowPanel() {
      contents += new Label { text = "Min Bet: $" }
      contents += Component.wrap(minSpinner)
      background = Constants.greenBack
    }
    val maxPanel = new FlowPanel() {
      contents += new Label { text = "Max Bet: $" }
      contents += Component.wrap(maxSpinner)
      background = Constants.greenBack
    }
    val dealerPanel = new FlowPanel() {
      contents += new Label { text = "Dealer: " }
      contents += dealerField
      background = Constants.greenBack
    }
    val buttonPanel = new FlowPanel() {
      contents += okButton
      contents += cancelButton
      background = Constants.greenBack
    }
    val contentPanel = new GridPanel(4,1) {
      contents += minPanel
      contents += maxPanel
      contents += dealerPanel
      contents += buttonPanel
        }
    contents = new BorderPanel() {
      add(new Label { icon = new ImageIcon("images/BJLogo2.gif") }, BorderPanel.Position.North)
      add(contentPanel,BorderPanel.Position.Center)
      border = Swing.EmptyBorder(20)
      background = Constants.greenBack
    }
    listenTo(okButton)
    listenTo(cancelButton)
    reactions += {
      case ButtonClicked(`okButton`) => 
        println("Clicked ok button") 
        // Invoke method sendMsg in EyeInTheSky to send a message to the House actor to add the table
        EyeInTheSky.sendMsg(AddTable(new Table(minSpinner.getValue().toString().toDouble,maxSpinner.getValue().toString().toDouble,dealerField.text)))
        this.close();       
      case ButtonClicked(`cancelButton`) => 
        this.close();
    }
  visible = true
}